package com.zys.lease.web.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zys.lease.model.entity.LeaseAgreement;
import com.zys.lease.model.enums.LeaseStatus;
import com.zys.lease.web.admin.service.LeaseAgreementService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RequiredArgsConstructor
public class ScheduledTasks {

    private final LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0 0 0 * * *")//每天的零时零分零秒执行
    public void checkLeaseStatus() {

//        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
//        Date now = new Date();
//        updateWrapper.le(LeaseAgreement::getLeaseEndDate, now);
//        updateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
//        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
//        leaseAgreementService.update(updateWrapper);

        leaseAgreementService.lambdaUpdate()
                .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED)
                .le(LeaseAgreement::getLeaseEndDate, new Date())
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING)
                .update();
    }
}